/**
 * @description: 日志存储
 */
import log4js from "log4js";
import { ErrorOptions } from "./typing";
import { response } from "@/utils/api";

// log4js 配置
log4js.configure({
  appenders: {
    // logger 自定义，在categories中会用到
    console: { type: "console" },
    // error 自定义，在categories中会用到
    error: {
      type: "dateFile",
      filename: "./src/utils/log/logs/error",
      pattern: "yyyy-MM-dd.log",
      alwaysIncludePattern: true,
    },
    // info 自定义，在categories中会用到
    info: {
      type: "dateFile",
      filename: "./src/utils/log/logs/info",
      pattern: "yyyy-MM-dd.log",
      alwaysIncludePattern: true,
    },
  },
  categories: {
    default: { appenders: ["console"], level: "debug" },
    error: {
      appenders: ["error", "console"],
      level: "error",
    },
    info: {
      appenders: ["info", "console"],
      level: "info",
    },
  },
});
export const logger = {
  log: (message: any) => {
    const logger = log4js.getLogger("info");
    logger.info(message);
  },
  error: (options: ErrorOptions) => {
    const logger = log4js.getLogger("error");
    const { res, reject, message = "", error, code = 203 } = options;
    if (res) response(res, code, null, message);
    if (reject) reject({ error, message });
    logger.error(message);
    error && logger.error(error);
  },
};
